package middle;

import util.PrintUtils;

public class Solution_75 {
    public void sortColors(int[] nums) {
        int zeroIndex = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                swap(nums, i, zeroIndex);
                zeroIndex++;
            }
        }
        int twoIndex = nums.length - 1;
        for (int i = nums.length - 1; i >= zeroIndex; i--) {
            if (nums[i] == 2) {
                swap(nums, i, twoIndex);
                twoIndex--;
            }
        }
        for (int i = zeroIndex; i <= twoIndex; i++) {
            nums[i] = 1;
        }
    }

    private void swap(int[] nums, int i, int j) {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }

    public static void main(String[] args) {
        Solution_75 model = new Solution_75();
//        int[] ints = {2,0,2,1,1,0};
        int[] ints = {2,0,1};
        model.sortColors(ints);
        PrintUtils.arrayInt(ints);
    }
}
